import 'dart:io';

import 'package:core_package/core_package.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/ui/login/widget/login_input_widget.dart';

class RegisterPage extends StatefulWidget {
  static const String routeName = "/registerPage";

  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  bool selectProtocol = false;
  String? mobile;
  String? password;
  String? surePassword;
  String? code;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
        child: buildContent(context),
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
      ),
    );
  }

  Widget buildContent(BuildContext context) {
    return Container(
      height: SizeFill.screenHeight,
      width: SizeFill.screenWidth,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage(
            MyAssetsImages.setAssetsImagesPath("icon_login_bg"),
          ),
          fit: BoxFit.fill,
        ),
      ),
      child: SingleChildScrollView(
        child: Container(
          child: Column(
            children: [
              SizedBox(height: SizeFill.statusHeight! + SizeFill.setRx(20)),
              Row(
                children: [
                  Expanded(child: MySizeBox()),
                  InkWell(
                    onTap: () {
                      loginOnTap();
                    },
                    child: Text(
                      "已有账号",
                      style: TextStyle(
                        color: MyColors.formHex("#272727FF"),
                        fontSize: MyFont.font14,
                      ),
                    ),
                  ),
                  SizedBox(width: 20),
                ],
              ),
              SizedBox(height: SizeFill.setRx(40)),
              Image.asset(
                MyAssetsImages.setAssetsImagesPath("icon_logo_1"),
                width: SizeFill.setRx(131.67),
                height: SizeFill.setRx(124.33),
              ),
              SizedBox(height: SizeFill.setRx(48)),
              Text(
                "让 生 活 更 美 好",
                style: TextStyle(
                  fontFamily: "assets/fonts/ZhanKuWenYiTi_2.ttf",
                  fontWeight: MyFontWeight.regular,
                  color: MyColors.formHex("#272727FF"),
                  fontSize: SizeFill.setRx(16),
                ),
              ),
              MySizeBox(height: 30),
              buildInputContent(),
              MySizeBox(height: 20),
            ],
          ),
        ),
      ),
    );
  }

  Widget buildInputContent() {
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(SizeFill.setRx(7)),
      ),
      margin: EdgeInsets.symmetric(horizontal: SizeFill.setRx(17)),
      child: Padding(
        padding: EdgeInsets.only(
          left: SizeFill.setRx(37),
          right: SizeFill.setRx(37),
          top: SizeFill.setRx(30),
          bottom: SizeFill.setRx(38),
        ),
        child: buildInputList(),
      ),
    );
  }

  Widget buildInputList() {
    return Column(
      children: [
        Text(
          "注册",
          style: TextStyle(
            fontSize: MyFont.font14,
            color: MyColors.formHex("#272727FF"),
            fontWeight: MyFontWeight.bold,
          ),
        ),
        MySizeBox(height: 30),
        LoginInputWidget(
          maxLength: 11,
          placeholder: "请输入手机号",
          leftIconWidget: Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(horizontal: SizeFill.setRx(10)),
            child: Text(
              "+86",
              style: TextStyle(
                color: MyColors.formHex("#272727FF"),
                fontSize: MyFont.font12,
              ),
            ),
          ),
          keyboardType: TextInputType.number,
          inputTextTapChange: (value) {
            mobile = value;
          },
        ),
        MySizeBox(height: 13),
        Row(
          children: [
            Expanded(
              child: LoginInputWidget(
                maxLength: 6,
                placeholder: "请输入验证码",
                leftIconWidget: Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.symmetric(horizontal: SizeFill.setRx(10)),
                  child: Text(
                    "验证码",
                    style: TextStyle(
                      color: MyColors.formHex("#272727FF"),
                      fontSize: MyFont.font12,
                    ),
                  ),
                ),
                keyboardType: TextInputType.number,
                inputTextTapChange: (value) {
                  code = value;
                },
              ),
            ),
            MySizeBox(width: 5),
            Container(
              color: MyColors.formHex("#F8BA35FF"),
              child: ShowSendCodeWidget(
                sendCodeType: SendCodeType.register,
                fontSize: MyFont.font12,
                textColor: Colors.white,
                norTextColor: Colors.white,
                minWidth: SizeFill.setRx(96),
                height: SizeFill.setRx(40),
                phone: mobile,
                bgColor: MyColors.formHex("#F8BA35FF"),
                sendCodeOnTap: () async {
                  return true;
                },
              ),
            )
          ],
        ),
        MySizeBox(height: 13),
        LoginInputWidget(
          maxLength: 20,
          placeholder: "请输入密码",
          leftIconWidget: Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(horizontal: SizeFill.setRx(10)),
            child: Text(
              "输入密码",
              style: TextStyle(
                color: MyColors.formHex("#272727FF"),
                fontSize: MyFont.font12,
              ),
            ),
          ),
          keyboardType: TextInputType.visiblePassword,
          inputTextTapChange: (value) {
            password = value;
          },
          obscureText: true,
        ),
        MySizeBox(height: 13),
        LoginInputWidget(
          maxLength: 20,
          placeholder: "请再次输入密码",
          leftIconWidget: Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(horizontal: SizeFill.setRx(10)),
            child: Text(
              "确认密码",
              style: TextStyle(
                color: MyColors.formHex("#272727FF"),
                fontSize: MyFont.font12,
              ),
            ),
          ),
          keyboardType: TextInputType.visiblePassword,
          inputTextTapChange: (value) {
            surePassword = value;
          },
          obscureText: true,
        ),
        MySizeBox(height: 25),
        buildProtocolWidget(),
        MySizeBox(height: 25),
        buildButton("注册", () {
          registerOnTap();
        }),
      ],
    );
  }

  Widget buildButton(String title, Function() onTap) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        onTap();
      },
      child: Container(
        width: SizeFill.setRx(267),
        height: SizeFill.setRx(40),
        decoration: BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fill,
            image: AssetImage(
              MyAssetsImages.setAssetsImagesPath("icon_button_bg_1"),
            ),
          ),
        ),
        alignment: Alignment.center,
        child: Text(
          title,
          style: TextStyle(
            color: MyColors.formHex("#FFFFFFFF"),
            fontSize: MyFont.font14,
            fontWeight: MyFontWeight.bold,
          ),
        ),
      ),
    );
  }

  Widget buildProtocolWidget() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            setState(() {
              selectProtocol = !selectProtocol;
            });
          },
          child: Container(
            padding: EdgeInsets.all(SizeFill.setRx(5)),
            child: Image.asset(
              MyAssetsImages.setAssetsImagesPath(selectProtocol ? "icon_select" : "icon_unselect"),
              width: SizeFill.setRx(14),
              height: SizeFill.setRx(14),
            ),
          ),
        ),
        Text(
          "我已阅读，且完全同意并接受《XXXX》",
          style: TextStyle(
            color: MyColors.formHex("#272727FF"),
            fontSize: MyFont.font10,
          ),
        )
      ],
    );
  }

  void loginOnTap() {
    NavigatorUtil.pop(context);
  }

  void registerOnTap() {}
}
